<!--  -->
<template>
  <div class="vr">
    <div class="header">
      <van-icon name="arrow-left" size="18" @click="goBack()" />
      <p>vr看房</p>
      <span></span>
    </div>
    <div id="viewer"></div>
  </div>
</template>

<script>
import { Viewer } from "photo-sphere-viewer";
import MarkersPlugin from "photo-sphere-viewer/dist/plugins/markers";
import "photo-sphere-viewer/dist/photo-sphere-viewer.css";
import "photo-sphere-viewer/dist/plugins/markers.css";
export default {
  data() {
    return {
      viewer: "",
      imgurl1: require("../../assets/vr1.jpg"),
      imgurl2: require("../../assets/vr2.jpg"),
      imgurl3: require("../../assets/vr3.jpg"),
    };
  },
  mounted() {
    this.viewer = new Viewer({
      container: document.querySelector("#viewer"),
      panorama: this.imgurl1,
      size: {
        width: "100vw",
        height: "50vh",
      },
      plugins: [
        [
          MarkersPlugin,
          {
            markers: [
              {
                id: "circle",
                tooltip: "A circle of radius 30",
                circle: 30,
                svgStyle: {
                  fill: "rgba(255,255,0,0.3)",
                  stroke: "yellow",
                  strokeWidth: "2px",
                },
                longitude: -1.5,
                latitude: -0.28,
                anchor: "center right",
              },
            ],
          },
        ],
      ],
    });

    const markersPlugin = this.viewer.getPlugin(MarkersPlugin);

    markersPlugin.on("select-marker", (e, marker) => {
      this.viewer
        .animate({
          longitude: marker.config.longitude,
          latitude: marker.config.latitude,
          zoom: 100,
          speed: "-2rpm",
        })
        .then(() =>
          this.viewer.setPanorama(this.imgurl2).then(
            () =>
              markersPlugin.updateMarker({
                id: marker.id,
                longitude: -1.8,
                latitude: -0.28,
              }),
            this.viewer
              .animate({
                zoom: 50,
                speed: "2rpm",
              })
              .then(
                () => (this.imgurl2 = this.imgurl3),
                console.log("继续操作")
              )
          )
        );
    });
  },
  methods: {
    //返回上一级
    goBack() {
      this.$router.go(-1);
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style lang="scss" scoped>
.vr {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.header {
  width: 100%;
  height: 70px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 18px;
  color: #fff;
  background: #4381e6;
  padding: 0 15px;
}
</style>
